{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[![Jupyter Notebook预览](https://cdn.jsdelivr.net/gh/eternidad33/picbed/img/upyter%E9%A2%84%E8%A7%88.png)](https://nbviewer.jupyter.org/github/eternidad33/csp/blob/master/Python/题解汇总/第2题题解汇总.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 目录\n",
    "\n",
    "- [ISBN号码](#ISBN号码)\n",
    "- [窗口](#窗口)\n",
    "- [画图](#画图)\n",
    "- [Z字形扫描](#Z字形扫描)\n",
    "- [数字排序](#数字排序)\n",
    "- [日期计算](#日期计算)\n",
    "- [消除类游戏](#消除类游戏)\n",
    "- [俄罗斯方块](#俄罗斯方块)\n",
    "- [火车购票](#火车购票)\n",
    "- [工资计算](#工资计算)\n",
    "- [学生排队](#学生排队)\n",
    "- [公共钥匙盒](#公共钥匙盒)\n",
    "- [游戏](#游戏)\n",
    "- [碰撞的小球](#碰撞的小球)\n",
    "- [买菜](#买菜)\n",
    "- [小明放学](#小明放学)\n",
    "- [二十四点](#二十四点)\n",
    "- [小明种苹果续](#小明种苹果续)\n",
    "- [回收站选址](#回收站选址)\n",
    "- [稀疏向量](#稀疏向量)\n",
    "- [风险人群筛查](#风险人群筛查)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ISBN号码\n",
    "\n",
    "试题编号：\t201312-2  \n",
    "试题名称：\tISBN号码\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Right\n"
     ]
    }
   ],
   "source": [
    "def backlist(s):\n",
    "    '''将字符串提取为整型数组'''\n",
    "    rlist = []\n",
    "    for i in s:\n",
    "        if i.isdigit():\n",
    "            rlist.append(int(i))\n",
    "    return rlist\n",
    "\n",
    "\n",
    "# s = input()\n",
    "s= \"0-670-82162-4\"\n",
    "l = backlist(s)\n",
    "sum = 0\n",
    "for i in range(9):\n",
    "    sum += l[i] * (i + 1)\n",
    "code = sum % 11\n",
    "if s[-1] == str(code) or (s[-1] == 'X' and code == 10):\n",
    "    print(\"Right\")\n",
    "else:\n",
    "    if code == 10:\n",
    "        s = s[:-1] + 'X'\n",
    "    else:\n",
    "        s = s[:-1] + str(code)\n",
    "    print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 窗口\n",
    "\n",
    "试题编号：\t201403-2  \n",
    "试题名称：\t窗口\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def chuangkou(window, point_x, point_y):\n",
    "    \"\"\"识别窗口\"\"\"\n",
    "    global i, j\n",
    "    find = False\n",
    "    for i, s in reversed(list(enumerate(window))):\n",
    "        if s[0] <= point_x <= s[2] and s[1] <= point_y <= s[3]:\n",
    "            print(s[4])\n",
    "            last = window[i]\n",
    "            for j in range(i, len(window) - 1):\n",
    "                window[j] = window[j + 1]\n",
    "            window[-1] = last\n",
    "            find = True\n",
    "            break\n",
    "    if not find:\n",
    "        print('IGNORED')\n",
    "\n",
    "\n",
    "# 录入数据\n",
    "# n, m = map(int, input().split())\n",
    "# w = []\n",
    "# for i in range(n):\n",
    "#     w.append(list(map(int, input().split())))\n",
    "#     w[i].append(i + 1)\n",
    "# for j in range(m):\n",
    "#     x, y = list(map(int, input().split()))\n",
    "#     chuangkou(w, x, y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 画图\n",
    "\n",
    "试题编号：\t201409-2  \n",
    "试题名称：\t画图\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def huaTu():\n",
    "    n = int(input())\n",
    "    pnum = [[0] * 100 for _ in range(100)]\n",
    "    for i in range(n):\n",
    "        x1, y1, x2, y2 = map(int, input().split())\n",
    "        for x in range(x1, x2):\n",
    "            for y in range(y1, y2):\n",
    "                # 将被覆盖的区域置为1\n",
    "                pnum[x][y] = 1\n",
    "    area = 0\n",
    "    for i in range(100):\n",
    "        area += sum(pnum[i])\n",
    "    print(area)\n",
    "\n",
    "\n",
    "# huaTu()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Z字形扫描\n",
    "\n",
    "试题编号：\t201412-2  \n",
    "试题名称：\tZ字形扫描\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def z(m, num):\n",
    "    # up用于存储是否向上遍历，默认向上遍历\n",
    "    up = True\n",
    "    line = []\n",
    "    i, j = 0, 0\n",
    "    for _ in range(m * m):\n",
    "        line.append(num[i][j])\n",
    "        # 处理向上移动\n",
    "        if up:\n",
    "            # 处理碰到右边界，下移\n",
    "            if j == m - 1:\n",
    "                i += 1\n",
    "                up = False\n",
    "            # 处理碰到上边界，右移\n",
    "            elif i == 0:\n",
    "                j += 1\n",
    "                up = False\n",
    "            # 其他情况，右上\n",
    "            else:\n",
    "                j += 1\n",
    "                i -= 1\n",
    "        else:\n",
    "            if i == m - 1:\n",
    "                j += 1\n",
    "                up = True\n",
    "            elif j == 0:\n",
    "                i += 1\n",
    "                up = True\n",
    "            else:\n",
    "                j -= 1\n",
    "                i += 1\n",
    "    print(\" \".join(map(str, line)))\n",
    "\n",
    "\n",
    "# n = int(input())\n",
    "# pnum = []\n",
    "# for i in range(n):\n",
    "#     pnum.append(list(map(int, input().split())))\n",
    "# z(n, pnum)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数字排序\n",
    "\n",
    "试题编号：\t201503-2  \n",
    "试题名称：\t数字排序\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def shuZiPaiXu(nums):\n",
    "    nums.sort()\n",
    "    a = {}\n",
    "    for i in nums:\n",
    "        a[i] = nums.count(i)\n",
    "    # 排序\n",
    "    a = sorted(a.items(), key=lambda item: item[1], reverse=True)\n",
    "    for j in a:\n",
    "        print(j[0], j[1])\n",
    "\n",
    "\n",
    "# n = int(input())\n",
    "# ls = list(map(int, input().split()))\n",
    "# shuZiPaiXu(ls)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 日期计算\n",
    "\n",
    "试题编号：\t201509-2  \n",
    "试题名称：\t日期计算\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def riQiJiSuan(year, day):\n",
    "    mon = 0\n",
    "    md = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "    # 判断闰年\n",
    "    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):\n",
    "        md[2] = 29\n",
    "    for i in md:\n",
    "        if day - i > 0:\n",
    "            day -= i\n",
    "            mon += 1\n",
    "        else:\n",
    "            break\n",
    "    print(mon)\n",
    "    print(day)\n",
    "\n",
    "\n",
    "# y = int(input())\n",
    "# d = int(input())\n",
    "# riQiJiSuan(y, d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 消除类游戏\n",
    "\n",
    "试题编号：\t201512-2  \n",
    "试题名称：\t消除类游戏\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def xiaoChuLeiYouXi(A, n, m):\n",
    "    # B中元素为1，代表在A中连续\n",
    "    B = [[0] * m for _ in range(n)]\n",
    "    global i\n",
    "    for i in range(n):\n",
    "        for j in range(m):\n",
    "            # 竖排三个相同\n",
    "            if 0 < i < n - 1 and A[i - 1][j] == A[i][j] and A[i][j] == A[i + 1][j]:\n",
    "                B[i - 1][j] = 1\n",
    "                B[i][j] = 1\n",
    "                B[i + 1][j] = 1\n",
    "            # 横排三个相同\n",
    "            if 0 < j < m - 1 and A[i][j - 1] == A[i][j] and A[i][j] == A[i][j + 1]:\n",
    "                B[i][j - 1] = 1\n",
    "                B[i][j] = 1\n",
    "                B[i][j + 1] = 1\n",
    "    for i in range(n):\n",
    "        for j in range(m):\n",
    "            if B[i][j] == 1:\n",
    "                A[i][j] = 0\n",
    "            print(A[i][j], end=\" \")\n",
    "        print()\n",
    "\n",
    "\n",
    "# n, m = map(int, input().split())\n",
    "# lm = []\n",
    "# for i in range(n):\n",
    "#     lm.append(list(map(int, input().split())))\n",
    "# xiaoChuLeiYouXi(lm, n, m)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 俄罗斯方块\n",
    "\n",
    "试题编号：\t201604-2  \n",
    "试题名称：\t俄罗斯方块\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def eLuoSiFangKuai(tmap, shape, x):\n",
    "    global i, j\n",
    "    # 将形状转换为4行10列\n",
    "    for i in range(4):\n",
    "        shape[i] = [0] * (x - 1) + shape[i] + [0] * (6 - (x - 1))\n",
    "    # 图形第一行最终落到的方格图对应的行数\n",
    "    row1 = 0\n",
    "    find = False\n",
    "    for i in range(3):\n",
    "        tmap.append([1] * 10)\n",
    "    for i in range(15):\n",
    "        for j in range(4):\n",
    "            newline = [a + b for a, b in zip(tmap[i + j], shape[j])]\n",
    "            if 2 in newline:\n",
    "                row1 = i - 1\n",
    "                find = True\n",
    "                break\n",
    "        if find:\n",
    "            break\n",
    "        if i == 14:\n",
    "            row1 = 14\n",
    "    # 拼接数据\n",
    "    for i in range(4):\n",
    "        tmap[row1 + i] = [a + b for a, b in zip(tmap[row1 + i], shape[i])]\n",
    "    # 输出数据\n",
    "    for i in range(15):\n",
    "        print(' '.join(map(str, tmap[i])))\n",
    "\n",
    "\n",
    "# # 录入数据\n",
    "# tmap, shape = [], []\n",
    "# for i in range(15):\n",
    "#     temp_a = list(map(int, input().split()))\n",
    "#     tmap.append(temp_a)\n",
    "# for j in range(4):\n",
    "#     temp_b = list(map(int, input().split()))\n",
    "#     shape.append(temp_b)\n",
    "# x = int(input())\n",
    "# eLuoSiFangKuai(tmap, shape, x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 火车购票\n",
    "\n",
    "试题编号：\t201609-2  \n",
    "试题名称：\t火车购票\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def huoCheGouPiao(count, l):\n",
    "    \"\"\"火车购票\n",
    "\n",
    "    思路：开辟一个二维数组存取剩余座位号\n",
    "    \"\"\"\n",
    "    num = []  # 存座位号\n",
    "    # 为num编号\n",
    "    for i in range(20):\n",
    "        temp = []\n",
    "        for j in range(5):\n",
    "            temp.append(i * 5 + j + 1)\n",
    "        num.append(temp)\n",
    "\n",
    "    seats = [5 for i in range(20)]  # 每一行的剩余座位\n",
    "    p = -1  # 哪一行的座位数够用\n",
    "    res = [[] for i in range(count)]  # 存取最后结果\n",
    "    a = 0  # res的下标\n",
    "    for i in l:\n",
    "        for j in range(20):\n",
    "            if seats[j] >= i:\n",
    "                p = j\n",
    "                break\n",
    "        if p != -1:\n",
    "            for k in range(5):\n",
    "                if i == 0:\n",
    "                    break\n",
    "                if num[p][k] != 0:\n",
    "                    res[a].append(num[p][k])\n",
    "                    num[p][k] = 0\n",
    "                    seats[p] -= 1\n",
    "                    i -= 1\n",
    "            a += 1\n",
    "        else:\n",
    "            # 找不到行数，则应该安排在编号最小的几个空座位中（不考虑是否相邻）\n",
    "            for j in range(20):\n",
    "                for k in range(5):\n",
    "                    if i == 0:\n",
    "                        a += 1\n",
    "                        break\n",
    "                    if num[j][k] != 0:\n",
    "                        res[a].append(num[j][k])\n",
    "                        num[j][k] = 0\n",
    "                        seats[j] -= 1\n",
    "                        i -= 1\n",
    "                if i == 0:\n",
    "                    break\n",
    "        p = -1\n",
    "    for i in res:\n",
    "        for j in i:\n",
    "            print(j, end=' ')\n",
    "        print()\n",
    "\n",
    "\n",
    "# n = int(input())\n",
    "# m = list(map(int, input().split()))\n",
    "# huoCheGouPiao(n, m)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 工资计算\n",
    "\n",
    "试题编号：\t201612-2  \n",
    "试题名称：\t工资计算\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10000\n"
     ]
    }
   ],
   "source": [
    "t = 9255\n",
    "# t = int(input())\n",
    "rate = [0, 0.03, 0.1, 0.2, 0.25, 0.3, 0.35, 0.45]\n",
    "sA = [0, 1500, 4500, 9000, 35000, 55000, 80000, 100000]\n",
    "afterTax = [3500]  # 税后工资\n",
    "sum0 = 0\n",
    "for i in range(1, len(sA)):\n",
    "    sum0 += int((sA[i] - sA[i - 1]) * (1 - rate[i]))\n",
    "    afterTax.append(sum0 + 3500)\n",
    "if t <= 3500:\n",
    "    s = t\n",
    "else:\n",
    "    for i in range(len(afterTax)):\n",
    "        if afterTax[i] < t <= afterTax[i + 1]:\n",
    "            s = 3500 + sA[i]\n",
    "            s += int((t - afterTax[i]) / (1 - rate[i + 1]))\n",
    "            # 保证小明的税前工资为一个整百的数\n",
    "            s = 100 * (round(s / 100))\n",
    "            break\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 学生排队\n",
    "\n",
    "试题编号：\t201703-2  \n",
    "试题名称：\t学生排队\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def 学生排队():\n",
    "    n = int(input())\n",
    "    q = list(range(1, n + 1))\n",
    "    m = int(input())\n",
    "    for i in range(m):\n",
    "        k, t = map(int, input().split())\n",
    "        after = q.index(k) + t  # 移动后的索引\n",
    "        q.remove(k)  # 删除\n",
    "        q.insert(after, k)  # 插入\n",
    "    print(' '.join(map(str, q)))\n",
    "\n",
    "\n",
    "# 学生排队()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公共钥匙盒\n",
    "\n",
    "试题编号：\t201709-2  \n",
    "试题名称：\t公共钥匙盒\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def return_lend_key(key_info, times, time):\n",
    "    \"\"\"time在times借用或者归还的钥匙\"\"\"\n",
    "    RL_key = []\n",
    "    for i in range(len(times)):\n",
    "        if times[i] == time:\n",
    "            RL_key.append(key_info[i][0])\n",
    "    return RL_key\n",
    "\n",
    "\n",
    "def 公共钥匙盒(key_len, key_info):\n",
    "    global i\n",
    "    key = list(range(1, key_len + 1))\n",
    "    # 分别存储开始使用时间和归还时间\n",
    "    start_time = [i[1] for i in key_info]\n",
    "    end_time = [i[1] + i[2] for i in key_info]\n",
    "    for t in range(1, max(end_time) + 1):\n",
    "        # 先还后借\n",
    "        # t时刻为归还时间\n",
    "        if t in end_time:\n",
    "            return_key = return_lend_key(key_info, end_time, t)\n",
    "            return_key.sort()\n",
    "            for r in return_key:\n",
    "                for i in range(key_len):\n",
    "                    if key[i] == 0:\n",
    "                        key[i] = r\n",
    "                        break\n",
    "        # t时刻为借用时间\n",
    "        if t in start_time:\n",
    "            lend_key = return_lend_key(key_info, start_time, t)\n",
    "            for r in lend_key:\n",
    "                for i in range(key_len):\n",
    "                    if key[i] == r:\n",
    "                        key[i] = 0\n",
    "    print(' '.join(map(str, key)))\n",
    "\n",
    "\n",
    "# n, k = map(int, input().split())\n",
    "# info = []\n",
    "# for i in range(k):\n",
    "#     info.append(list(map(int, input().split())))\n",
    "# 公共钥匙盒(n, info)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 游戏\n",
    "\n",
    "试题编号：\t201712-2  \n",
    "试题名称：\t游戏\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "n, k = 5, 2\n",
    "# n, k = map(int, input().split())\n",
    "a = list(range(1, n + 1))\n",
    "num = 1\n",
    "while len(a) > 0:\n",
    "    key_del = []\n",
    "    # key_del存储要删除元素的索引\n",
    "    for key, value in enumerate(a):\n",
    "        # key存储的是a的索引\n",
    "        if num % k == 0 or num % 10 == k:\n",
    "            key_del.append(key)\n",
    "        num += 1\n",
    "    ac = 0\n",
    "    # 由于删除元素列表的索引会跟随变化\n",
    "    # ac存储的是删除元素的个数\n",
    "    for key in key_del:\n",
    "        del a[key - ac]\n",
    "        ac += 1\n",
    "        if len(a) == 1:\n",
    "            print(a[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 碰撞的小球\n",
    "\n",
    "试题编号：\t201803-2  \n",
    "试题名称：\t碰撞的小球\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "def 碰撞的小球(count, axis_len, time, position):\n",
    "    dire = [1] * count  # 存储运动方向\n",
    "    for ti in range(time):\n",
    "        for p in range(count):\n",
    "            if position[p] == axis_len:\n",
    "                dire[p] = -1\n",
    "            if position[p] == 0:\n",
    "                dire[p] = 1\n",
    "            position[p] += dire[p]\n",
    "        # 查看小球是否相撞\n",
    "        for p in range(count):\n",
    "            for q in range(p + 1, count):\n",
    "                if position[p] == position[q]:\n",
    "                    dire[p] = -dire[p]\n",
    "                    dire[q] = -dire[q]\n",
    "    print(\" \".join(str(i) for i in position))\n",
    "\n",
    "# n, L, t = map(int, input().split())\n",
    "# POS = list(map(int, input().split()))  # 存储每个小球位置\n",
    "# 碰撞的小球(n, L, t, POS)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 买菜\n",
    "\n",
    "试题编号：\t201809-2  \n",
    "试题名称：\t买菜\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "def 买菜():\n",
    "    n = int(input())\n",
    "    H, W = [], []\n",
    "    for i in range(n):\n",
    "        H.append(list(map(int, input().split())))\n",
    "    for i in range(n):\n",
    "        W.append(list(map(int, input().split())))\n",
    "    tlen = max(H[-1][-1], W[-1][-1])\n",
    "    # 初始化时间轴\n",
    "    timeLine = [0] * tlen\n",
    "    for s, e in H:\n",
    "        for i in range(s, e):\n",
    "            timeLine[i] += 1\n",
    "    for s, e in W:\n",
    "        for i in range(s, e):\n",
    "            timeLine[i] += 1\n",
    "    print(timeLine.count(2))\n",
    "\n",
    "\n",
    "# 买菜()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 小明放学\n",
    "\n",
    "试题编号：\t201812-2  \n",
    "试题名称：\t小明放学\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "def 小明放学():\n",
    "    r, y, g = map(int, input().split())\n",
    "    n = int(input())\n",
    "    tsum = 0\n",
    "    for i in range(n):\n",
    "        k, t = map(int, input().split())\n",
    "        if k == 0:\n",
    "            tsum += t\n",
    "            continue\n",
    "        if k == 1:\n",
    "            t += g\n",
    "        if k == 2:\n",
    "            t += (r + g)\n",
    "        nowtime = (t - tsum) % (r + y + g)\n",
    "        # (t-tsum)为倒计时绝对时间取余一次循环时间得出当前循环距绿灯结束的时间\n",
    "        if nowtime > g:\n",
    "            tsum += (nowtime - g)\n",
    "    print(tsum)\n",
    "\n",
    "\n",
    "# 小明放学()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二十四点\n",
    "\n",
    "试题编号：\t201903-2  \n",
    "试题名称：\t二十四点\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def 二十四点(s0):\n",
    "    for a in s0:\n",
    "        a = a.replace(\"x\", \"*\")\n",
    "        a = a.replace(\"/\", \"//\")\n",
    "        sum_a = eval(a)\n",
    "        if sum_a == 24:\n",
    "            print(\"Yes\")\n",
    "        else:\n",
    "            print(\"No\")\n",
    "\n",
    "\n",
    "# # 录入数据\n",
    "# n = int(input())\n",
    "# s = []\n",
    "# for i in range(n):\n",
    "#     s.append(input())\n",
    "# 二十四点(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 小明种苹果续\n",
    "\n",
    "试题编号：\t201909-2  \n",
    "试题名称：\t小明种苹果（续）\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "def 小明种苹果续():\n",
    "    n = int(input())\n",
    "    T, D, E = 0, 0, 0\n",
    "    lost_index = []\n",
    "    for i in range(n):\n",
    "        s = list(map(int, input().split()))\n",
    "        a0, am = s[0], s[1:]\n",
    "        b = False\n",
    "        temp = am[0]\n",
    "        for j in am:\n",
    "            if j > 0:\n",
    "                if j != temp:\n",
    "                    temp = j\n",
    "                    b = True\n",
    "            else:\n",
    "                temp += j\n",
    "        if b:\n",
    "            lost_index.append(i)\n",
    "            D += 1\n",
    "        T += temp\n",
    "    t = [0] * n\n",
    "    for i in lost_index:\n",
    "        t[i] = 1\n",
    "    if t[-2] == t[-1] == t[0] == 1:\n",
    "        E += 1\n",
    "    if t[-1] == t[0] == t[1] == 1:\n",
    "        E += 1\n",
    "    for i in range(1, n - 1):\n",
    "        if t[i - 1] == t[i] == t[i + 1] == 1:\n",
    "            E += 1\n",
    "    print(T, D, E)\n",
    "\n",
    "\n",
    "# 小明种苹果续()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 回收站选址\n",
    "\n",
    "试题编号：\t201912-2  \n",
    "试题名称：\t回收站选址\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "def 回收站选址():\n",
    "    n = int(input())\n",
    "    points, re = [], []\n",
    "    for i in range(n):\n",
    "        x, y = map(int, input().split())\n",
    "        points.append([x, y])\n",
    "    for p in points:\n",
    "        if ([p[0] - 1, p[1]] in points) and ([p[0] + 1, p[1]] in points) and ([p[0], p[1] + 1] in points) and (\n",
    "                [p[0], p[1] - 1] in points):\n",
    "            re.append(p)\n",
    "    sc = [0, 0, 0, 0, 0]\n",
    "    for r in re:\n",
    "        score = 0\n",
    "        if [r[0] - 1, r[1] - 1] in points:\n",
    "            score += 1\n",
    "        if [r[0] - 1, r[1] + 1] in points:\n",
    "            score += 1\n",
    "        if [r[0] + 1, r[1] - 1] in points:\n",
    "            score += 1\n",
    "        if [r[0] + 1, r[1] + 1] in points:\n",
    "            score += 1\n",
    "        sc[score] += 1\n",
    "    for i in range(5):\n",
    "        print(sc[i])\n",
    "\n",
    "\n",
    "# 回收站选址()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 稀疏向量\n",
    "\n",
    "试题编号：\t202006-2  \n",
    "试题名称：\t稀疏向量\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "def 稀疏向量():\n",
    "    # # 方法1，使用列表，运行超时，60分\n",
    "    # n, p, q = map(int, input().split())\n",
    "    # u = [0 for _ in range(n)]\n",
    "    # v = [0 for _ in range(n)]\n",
    "    # for i in range(p):\n",
    "    #     index, value = map(int, input().split())\n",
    "    #     u[index - 1] = value\n",
    "    # for j in range(q):\n",
    "    #     index, value = map(int, input().split())\n",
    "    #     v[index - 1] = value\n",
    "    # uv = 0  # 内积\n",
    "    # for k in range(n):\n",
    "    #     uv += u[k] * v[k]\n",
    "    # print(uv)\n",
    "    \n",
    "    # 方法2，使用字典，运行超时，60分\n",
    "    n, p, q = map(int, input().split())\n",
    "    u = {}  # 字典存储u向量索引对应的值\n",
    "    for i in range(p):\n",
    "        index, value = map(int, input().split())\n",
    "        u[index] = value\n",
    "    uv = 0  # 内积\n",
    "    for j in range(q):\n",
    "        index, value = map(int, input().split())\n",
    "        if index in u.keys():\n",
    "            uv += value * u[index]\n",
    "    print(uv)\n",
    "\n",
    "\n",
    "# 稀疏向量()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Python一直超时，下面是一个满分的Java代码**\n",
    "\n",
    "> Java使用Scanner读取数据也容易出现超时，具体设计步骤可看文章[《使用Java语言刷OJ经常超时的解决办法》](https://blog.csdn.net/richenyunqi/article/details/84350768)  \n",
    "> 代码参考文章[《CCF计算机软件能力认证试题练习：202006-2 稀疏向量 java100分代码》](https://blog.csdn.net/a598853372/article/details/108178485)\n",
    "\n",
    "\n",
    "```java\n",
    "import java.io.BufferedReader;\n",
    "import java.io.IOException;\n",
    "import java.io.InputStreamReader;\n",
    "import java.util.HashMap;\n",
    "import java.util.StringTokenizer;\n",
    "\n",
    "public class Main {\n",
    "\n",
    "    public static void main(String[] args){\n",
    "        int n=0, a=0, b=0;\n",
    "        try {\n",
    "            n = Reader.nextInt();\n",
    "            a = Reader.nextInt();\n",
    "            b = Reader.nextInt();\n",
    "        } catch (IOException e) {\n",
    "            e.printStackTrace();\n",
    "        }\n",
    "        HashMap<Integer,Integer> arr=new HashMap<>();\n",
    "\n",
    "\n",
    "        long sum=0;\n",
    "\n",
    "        int k=0,g=0;\n",
    "        for(int i=0;i<a;i++)\n",
    "        {\n",
    "            try {\n",
    "                k = Reader.nextInt();\n",
    "                g = Reader.nextInt();\n",
    "\n",
    "            } catch (IOException e) {\n",
    "                e.printStackTrace();\n",
    "            }\n",
    "            arr.put(k,g);\n",
    "        }\n",
    "        for(int i=0;i<b;i++)\n",
    "        {\n",
    "            try {\n",
    "                k = Reader.nextInt();\n",
    "                g = Reader.nextInt();\n",
    "\n",
    "            } catch (IOException e) {\n",
    "                e.printStackTrace();\n",
    "            }\n",
    "            if(arr.containsKey(k))\n",
    "            {\n",
    "                sum+=arr.get(k)*g;\n",
    "            }\n",
    "\n",
    "        }\n",
    "        System.out.println(sum);\n",
    "   \n",
    "    }\n",
    "}\n",
    "class Reader {\n",
    "    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));\n",
    "    static StringTokenizer tokenizer = new StringTokenizer(\"\");\n",
    "    static String nextLine() throws IOException{// 读取下一行字符串\n",
    "        return reader.readLine();\n",
    "    }\n",
    "    static String next() throws IOException {// 读取下一个字符串\n",
    "        while (!tokenizer.hasMoreTokens()) {\n",
    "            tokenizer = new StringTokenizer(reader.readLine());\n",
    "        }\n",
    "        return tokenizer.nextToken();\n",
    "    }\n",
    "\n",
    "    static int nextInt() throws IOException {// 读取下一个int型数值\n",
    "        return Integer.parseInt(next());\n",
    "    }\n",
    "\n",
    "    static double nextDouble() throws IOException {// 读取下一个double型数值\n",
    "        return Double.parseDouble(next());\n",
    "    }\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## 风险人群筛查\n",
    "\n",
    "试题编号：\t202009-2  \n",
    "试题名称：\t风险人群筛查\n",
    "\n",
    "[返回目录](#目录)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "def trans(l):\n",
    "    w = []\n",
    "    i = 0\n",
    "    while i < len(l):\n",
    "        w.append([l[i], l[i + 1]])\n",
    "        i += 2\n",
    "    return w\n",
    "\n",
    "\n",
    "def 风险人群筛查():\n",
    "    global count\n",
    "    n, k, t, xl, yd, xr, yu = map(int, input().split())\n",
    "    l = []\n",
    "    for i in range(n):\n",
    "        l0 = list(map(int, input().split()))\n",
    "        l.append(trans(l0))\n",
    "    pc, dc = 0, 0\n",
    "    # pc：经过，dc：逗留\n",
    "    for li in l:\n",
    "        # 遍历每个人\n",
    "        count = 0\n",
    "        a = False\n",
    "        flag = False\n",
    "        lc = []\n",
    "        for i in range(t):\n",
    "            if xl <= li[i][0] <= xr and yd <= li[i][1] <= yu:\n",
    "                count += 1\n",
    "                a = True\n",
    "                flag = True\n",
    "                if count >= k:\n",
    "                    dc += 1\n",
    "                    break\n",
    "            else:\n",
    "                count = 0\n",
    "                flag = False\n",
    "        if a:\n",
    "            pc += 1\n",
    "    print(pc)\n",
    "    print(dc)\n",
    "\n",
    "\n",
    "# 风险人群筛查()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PyCharm (csp)",
   "language": "python",
   "name": "pycharm-b28dbbde"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
